package com.bangbang.controller;


import com.bangbang.configresult.Result;
import com.bangbang.configresult.ResultEnum;
import com.bangbang.feign.PayServiceFeign;
import com.bangbang.pojo.TbCourse;
import com.bangbang.pojo.TbOrder;
import com.bangbang.service.ITbOrderService;
import com.bangbang.util.IdWorker;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;


import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 * 订单表 前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2021-08-11
 */
@RestController
@RequestMapping("/bangbang/tbOrder")
@Api("订单服务")
public class TbOrderController {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private ITbOrderService tbOrderService;

    @Autowired
    private PayServiceFeign payServiceFeign;

//    订单服务的增加
    @PostMapping
    @ApiOperation("增加订单服务")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "uId",value = "用户ID", required = true,paramType = "query"),
            @ApiImplicitParam(name = "Preferential",value = "优惠卷金额", required = false,paramType = "query"),
            @ApiImplicitParam(name = "order",value = "实体类", required = true,paramType = "body"),
    })
    public Result save(@RequestParam(value = "uId",required = true) Long uid, @RequestBody TbOrder order, @RequestParam(value = "Preferential",required = false,defaultValue = "0")BigDecimal preferential ){
        Long id = new IdWorker().nextId();

//        根据用户id(用户ID是token带过来的)查询对应的缓存课程数据
        List<TbCourse> courseList = (List<TbCourse>)redisTemplate.boundHashOps("courseList").get(uid);
        if(courseList != null && courseList.size() > 0){
            for (TbCourse course : courseList) {
//                long l = new IdWorker().nextId();
                TbOrder tbOrder = new TbOrder();
//                tbOrder.setDerId(l);//订单号
                tbOrder.setDerName(order.getDerName());//订单名称
                tbOrder.setDerStatus(order.getDerStatus());//订单状态
                tbOrder.setDerMoney( order.getDerMoney().subtract(preferential));//订单金额
                tbOrder.setSpareA(uid.toString());//用户ID
                tbOrder.setSpareB(course.getCouTeacherId());//教师ID
                tbOrder.setSpareC(course.getCouId().toString());//课程ID
                tbOrderService.save(tbOrder);

            }
            String s = payServiceFeign.alipayTradePagePay(id, order.getDerMoney(), "老代", "老代二号");
            System.out.println(s);
            return Result.buildResult(ResultEnum.SUCCESS).setResult("s");
        }
        return Result.buildResult(ResultEnum.ERROR);
    }

//    订单服务的查询
    @GetMapping
    @ApiOperation("订单服务的查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "uid",value = "用户id",required = true,paramType = "query")
    })
    public Result select(@RequestParam(value = "uid",required = true)String uid){
        List<TbOrder> orderList = tbOrderService.selectUid(uid);
        return Result.buildResult(ResultEnum.SUCCESS).setResult(orderList);
    }


}

